// pages/management/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    traverList: [],
    personType: ['', '成人', '老人', '儿童', '婴儿'],
    isNull: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  getTravelerList(){
    let userInfo = util.isLogin();
    let that = this;
    util.http({
      url: '/api/miniwechat/center/people',
      headers: {
        token: userInfo.token
      },
      data: {
        user_id: userInfo.user_id
      }
    }).then(res => {
      if(res.length) {
        that.setData({
          traverList: res,
          isNull: false
        });
      } else {
        console.log("S")
        that.setData({
          isNull: true
        });
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 删除出行人
   */
  deletePerson(e){
    let userInfo = util.isLogin();
    let that = this;
    util.http({
      url: '/api/miniwechat/center/del_people',
      headers: {
        token: userInfo.token
      },
      data: {
        id: e.currentTarget.dataset.id,
        user_id: userInfo.user_id
      }
    }).then(res => {
      console.log("management deletePerson result------↓↓↓↓")
      console.log(res);
      that.getTravelerList();
    })
  },

  /**
   * 设置默认出行人
   */
  setDefaultTraveler(e){
    let userInfo = util.isLogin();
    let that = this;
    let selectId = e.currentTarget.dataset.id;
    util.http({
      url: '/api/miniwechat/center/default_people',
      headers: {
        token: userInfo.token
      },
      data: {
        id: selectId,
        user_id: userInfo.user_id
      }
    }).then(res => {
      console.log("management setDefaultTraveler result------↓↓↓↓")
      console.log(res);
      let tempList = that.data.traverList.map(item => {
        if (item.id == selectId){
          item.default = 1;
        }
        else{
          item.default = 0;
        }
        return item;
      })
      console.log(tempList)
      that.setData({
        traverList: tempList
      });
    })
  },

  editTravelerInfo(e){
    wx.navigateTo({
      url: '/pages/addTraveler/index?id=' + e.currentTarget.dataset.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getTravelerList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  goAddTraveler: function() {
    wx.navigateTo({
      url: '/pages/addTraveler/index'
    })
  }
})